<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends BaseController {

    public function index() {
        $this->display();
    }
    //用户注册
    public function register(){
        if(IS_POST){
            $username = I('username');
            $pwd = MD5(I('password'));
            $email = I('email');
            if ($username=='' || $pwd=='' || $email=='') {
                $this->error('请完整填写信息！');
            }
            $data = array(
                'username' =>$username,
                'password' =>$pwd,
                'email'=>$email,
                'picture' => '/Public/Home/images/logo.png'
            );
            $m=M('user');
            $res = $m->add($data);
            //$this->assign('waitSecond',3);
            if($res){
                //$this->success('注册成功,请登录！');
                $this->redirect('Index/index');
            }else{
                $this->error('注册失败！');
            }
        }
        $this->display();
    }
    //用户登录
    public function login(){
        //if(!IS_POST) halt('页面不存在');
        /*if(I('code','','md5')!=session('verify')){
            $this->error('验证码错误');
        }*/
        if(IS_POST){
            $username = I('username');
            $pwd = MD5(I('password'));
            $user = M('User')->where(array('username'=>$username))->find();
            if (!user || $user['password'] !=$pwd) {
                $this->error('账号或密码错误');

            }
            if ($user['lock']) $this->error('用户被锁定');

            $data = array(
                'id' =>$user['id'],
                'logintime' => time(),
                'loginip' => get_client_ip(),
            );
            M('user') ->save($data);
            session('uid',$user['id']);
            session('username',$user['username']);
            session('logintime',date('Y-m-d H:i:s',$user['logintime']));
            session('loginip',$user['loginip']);
            $this->redirect('Index/index');
        }
        $this->display();
    }
    //安全退出
    public function logout(){
        $_SESSION=array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name,'',time()-1,'/');
        }
        session_destroy();
        $this->success('退出成功！',U('Home/Index/index'));
    }

}
